/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;


/**
 * Manages the load and check of the file properties
 *
 * @author Davide Lorenzoli
 * @author Diego Bonura
 */
public class LoadProperties {
    /**
     * Loads properties and check it
     *
     * @param hermesHome The home directory of the current installation.
     * @param filePropertiesName the name of the properties file with the whole
     *        path.
     *
     * @return Return properties if everything is ok, otherwise null
     * @throws Exception The property file is malformed.
     * @throws FileNotFoundException The property file not exists.
     * @throws IOException Problems reading property file.
     */
    public static Properties loadProperties(String hermesHome, String filePropertiesName) throws FileNotFoundException, IOException, Exception {
        Properties properties = new Properties();
        File file = null;
        InputStream fis = new FileInputStream(hermesHome + "/conf/" + filePropertiesName);
        properties.load(fis);

        /*Puts the home directory as a properties*/
        properties.setProperty("HERMES.HOME_DIRECTORY", hermesHome);

        /*Starts to check every property*/
        

        /*Check the Store Path*/
        if (properties.getProperty("HERMES.STORE_DIRECTORY").indexOf("[HERMES.HOME]") != -1) {
            properties.setProperty("HERMES.STORE_DIRECTORY", hermesHome + properties.getProperty("HERMES.STORE_DIRECTORY").substring(properties.getProperty("HERMES.STORE_DIRECTORY").indexOf("]") + 1, properties.getProperty("HERMES.STORE_DIRECTORY").length()));
        }

        file = new File(properties.getProperty("HERMES.STORE_DIRECTORY"));

        if (!file.isDirectory()) {
            throw new Exception("HERMES.STORE_DIRECTORY property not a valid directory:" + properties.getProperty("HERMES.STORE_DIRECTORY"));
        }

        /*Check the logs parameter*/
        if (!properties.getProperty("HERMES.LOGS").equals("true") && !properties.getProperty("HERMES.LOGS").equals("false")) {
            throw new Exception("HERMES.LOGS property must be true or false (LowerCase)");
        }

        /*Check the logs class parameter*/
        if (properties.getProperty("HERMES.LOGS").equals("true")) {
            if (properties.getProperty("HERMES.LOGS_CLASS") == null) {
                throw new Exception("HERMES.LOGS_CLASS property must be a class (es: hermesV2.util.HermesConsoleLogger)");
            } 

            if (properties.getProperty("HERMES.LOGS_PROPERTYFILE") == null) {
                throw new Exception("HERMES.LOGS_PROPERTYFILE property must be a file (es: hermesfilelogger.properties)");
            }
            if (properties.getProperty("HERMES.LOGS_LEVEL")!=null){
            if (!properties.getProperty("HERMES.LOGS_LEVEL").equals("INFO") && !properties.getProperty("HERMES.LOGS_LEVEL").equals("DEBUG") && !properties.getProperty("HERMES.LOGS_LEVEL").equals("WARNING") && !properties.getProperty("HERMES.LOGS_LEVEL").equals("ERROR") && !properties.getProperty("HERMES.LOGS_LEVEL").equals("FATAL")){
                throw new Exception("HERMES.LOGS_LEVEL property must be FATAL,ERROR,WARNING,INFO or DEBUG");
            }}else{
                throw new Exception("HERMES.LOGS_LEVEL property must be defined");
            }
            /*Checks the HERMES.HOME substring*/
            if (properties.getProperty("HERMES.LOGS_PROPERTYFILE").indexOf("[HERMES.HOME]") != -1) {
                properties.setProperty("HERMES.LOGS_PROPERTYFILE", hermesHome + properties.getProperty("HERMES.LOGS_PROPERTYFILE").substring(properties.getProperty("HERMES.LOGS_PROPERTYFILE").indexOf("]") + 1, properties.getProperty("HERMES.LOGS_PROPERTYFILE").length()));
            }
            /*checks the file*/
            file = new File(properties.getProperty("HERMES.LOGS_PROPERTYFILE"));
            if (!file.isFile()) {
                throw new Exception("HERMES.LOGS_PROPERTYFILE property not a valid file:" + properties.getProperty("HERMES.LOGS_PROPERTYFILE"));
            }
        }
        
        /*Checks the net component class*/
        if (properties.getProperty("HERMES.NET_CLASS") == null) {
            throw new Exception("HERMES.NET_CLASS property must be a class (es: hermesV2.net.protocols.SendReceiveOpen)");
        } 
        
        /*Checks the net property file parameter*/
        if (properties.getProperty("HERMES.NET_PROPERTYFILE") == null) {
            throw new Exception("HERMES.NET_PROPERTYFILE property must be a file (es: hermesnet.properties)");
        } 
        /*Checks the HERMES.HOME substring*/
        if (properties.getProperty("HERMES.NET_PROPERTYFILE").indexOf("[HERMES.HOME]") != -1) {
            properties.setProperty("HERMES.NET_PROPERTYFILE", hermesHome + properties.getProperty("HERMES.NET_PROPERTYFILE").substring(properties.getProperty("HERMES.NET_PROPERTYFILE").indexOf("]") + 1, properties.getProperty("HERMES.NET_PROPERTYFILE").length()));
        }
        /*checks the file*/
        file = new File(properties.getProperty("HERMES.NET_PROPERTYFILE"));
        if (!file.isFile()) {
            throw new Exception("HERMES.NET_PROPERTYFILE property not a valid file:" + properties.getProperty("HERMES.NET_PROPERTYFILE"));
        }
        
        /*Check the save parameter*/
        if (!properties.getProperty("HERMES.SAVE_AGENTS").equals("true") && !properties.getProperty("HERMES.SAVE_AGENTS").equals("false")) {
            throw new Exception("HERMES.SAVE.AGENTS property must be true or false (LowerCase)");
        }

        /*Check the load service agent property*/
        if (!properties.getProperty("HERMES.LOAD_SERVICEAGENTS").equals("true") && !properties.getProperty("HERMES.LOAD_SERVICEAGENTS").equals("false")) {
            throw new Exception("LOAD_SERVICEAGENTS property must be true or false (LowerCase)");
        }
        if (properties.getProperty("HERMES.LOAD_SERVICEAGENTS").equals("true")){
        /*Check the Service Path*/
        if (properties.getProperty("HERMES.SERVICES_DIRECTORY").indexOf("[HERMES.HOME]") != -1) {
            properties.setProperty("HERMES.SERVICES_DIRECTORY", hermesHome + properties.getProperty("HERMES.SERVICES_DIRECTORY").substring(properties.getProperty("HERMES.SERVICES_DIRECTORY").indexOf("]") + 1, properties.getProperty("HERMES.SERVICES_DIRECTORY").length()));
        }

        file = new File(properties.getProperty("HERMES.SERVICES_DIRECTORY"));

        if (!file.isDirectory()) {
            throw new Exception("HERMES.SERVICES_DIRECTORY property not a valid directory:" + properties.getProperty("HERMES.SERVICES_DIRECTORY"));
        }
        }
        /*Check the GUI param*/
        if (properties.getProperty("HERMES.GUI") == null) {
            throw new Exception("HERMES.GUI property doesn't exist");
        } else {
            if (!properties.getProperty("HERMES.GUI").equals("true") && !properties.getProperty("HERMES.GUI").equals("false")) {
                throw new Exception("HERMES.GUI property must be true or false (LowerCase)");
            }
        }

        return properties;
    }
}
